﻿Imports USA411
Imports LabJack.LabJackUD.U3
Imports System.Data
Imports System.Data.SqlClient
Imports USA411.U3Config
Imports System.Text

Public Class U3Status

    Private WithEvents U30 As LJU3
    Private WithEvents U31 As LJU3
    Private WithEvents U32 As LJU3
    Private WithEvents U33 As LJU3
    Private WithEvents U34 As LJU3
    Private WithEvents U35 As LJU3

    Public ConfigForm As New PortConfig
    Public H2Form As New HS2_Test

    Private hs2 As USA411.HomeSeer2Engine
    Private InitDone As Boolean = False

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadUpU3s()
        Connect2HS2()
        SetUpUIControls()

        Timer1.Enabled = True
    End Sub

#Region "HS2 Routines Here"
    Private Sub ExecuteX10(ByVal DevCode As String, ByVal X10Cmd As String, ByVal Level As Integer)
        hs2.ExecuteX10(DevCode, X10Cmd, Level)
    End Sub

    Private Sub SetStatus(ByVal DevCode As String, ByVal Status As Integer)
        hs2.SetDeviceStatus(DevCode, Status)
    End Sub

    Private Sub SetValue(ByVal DevCode As String, ByVal Value As Integer)
        hs2.SetDeviceValue(DevCode, Value)
    End Sub

    Private Sub RunEvent(ByVal Evnt As String)
        hs2.RunEvent(Evnt)
    End Sub
 
#End Region

    Private Sub SetUpUIControls()
        Try
            U30 = New LJU3(ihandle(0), U3Cfg(0).Tables(0))
            Ctl_U3DI_Status1.Enabled = True
            Ctl_U3DI_Status1.Config = U3Cfg(0)
            Ctl_U3DI_Status1.pSerialNumber = pSerialNumbers(0)
            U30.Process()


            U31 = New LJU3(ihandle(1), U3Cfg(1).Tables(0))
            Ctl_U3DI_Status2.Enabled = True
            Ctl_U3DI_Status2.Config = U3Cfg(1)
            Ctl_U3DI_Status2.pSerialNumber = pSerialNumbers(1)
            U31.Process()

            U32 = New LJU3(ihandle(2), U3Cfg(2).Tables(0))
            Ctl_U3DI_Status2.Enabled = True
            Ctl_U3DI_Status3.Config = U3Cfg(2)
            Ctl_U3DI_Status3.pSerialNumber = pSerialNumbers(2)
            U32.Process()


            U33 = New LJU3(ihandle(3), U3Cfg(3).Tables(0))
            Ctl_U3DI_Status4.Enabled = True
            Ctl_U3DI_Status4.Config = U3Cfg(3)
            Ctl_U3DI_Status4.pSerialNumber = pSerialNumbers(3)
            U33.Process()

            U34 = New LJU3(ihandle(4), U3Cfg(4).Tables(0))
            Ctl_U3DI_Status5.Enabled = True
            Ctl_U3DI_Status5.Config = U3Cfg(4)
            Ctl_U3DI_Status5.pSerialNumber = pSerialNumbers(4)
            U34.Process()

            U35 = New LJU3(ihandle(5), U3Cfg(5).Tables(0))
            Ctl_U3DI_Status6.Enabled = True
            Ctl_U3DI_Status6.Config = U3Cfg(5)
            Ctl_U3DI_Status6.pSerialNumber = pSerialNumbers(5)
            U35.Process()

        Catch ex As Exception

        End Try
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If pNumFound > 5 Then
            U35.Process()
        End If
        If pNumFound > 4 Then
            U34.Process()
        End If
        If pNumFound > 3 Then
            U33.Process()
        End If
        If pNumFound > 2 Then
            U32.Process()
        End If
        If pNumFound > 1 Then
            U31.Process()
        End If
        If pNumFound > 0 Then
            U30.Process()
        End If
    End Sub

    Private Sub ExecuteAction(ByVal action As Integer())
        hs2.ProcessActions(action)
    End Sub

    Private Sub Connect2HS2()
        With HS2Connect.Tables(0).Rows(0)
            hs2 = New USA411.HomeSeer2Engine(.Item("Server"), .Item("USerID"), .Item("Password"))
            hs2.Actions = Actions.Tables(0)
        End With
    End Sub

#Region "Interupt update from Devices (U3's)"

    Private Sub U30_ChangeStatus(ByVal action As Integer(), ByVal status As Boolean()) Handles U30.ChangeStatus
        Ctl_U3DI_Status1.Status = status
        ExecuteAction(action)
    End Sub

    Private Sub U31_ChangeStatus(ByVal action As Integer(), ByVal status As Boolean()) Handles U31.ChangeStatus
        Ctl_U3DI_Status2.Status = status
        ExecuteAction(action)
    End Sub

    Private Sub U32_ChangeStatus(ByVal action As Integer(), ByVal status As Boolean()) Handles U32.ChangeStatus
        Ctl_U3DI_Status3.Status = status
        ExecuteAction(action)
    End Sub

    Private Sub U33_ChangeStatus(ByVal action As Integer(), ByVal status As Boolean()) Handles U33.ChangeStatus
        Ctl_U3DI_Status4.Status = status
        ExecuteAction(action)
    End Sub

    Private Sub U34_ChangeStatus(ByVal action As Integer(), ByVal status As Boolean()) Handles U34.ChangeStatus
        Ctl_U3DI_Status5.Status = status
        ExecuteAction(action)
    End Sub

    Private Sub U35_ChangeStatus(ByVal action As Integer(), ByVal status As Boolean()) Handles U35.ChangeStatus
        Ctl_U3DI_Status6.Status = status
        ExecuteAction(action)
    End Sub

#End Region

#Region "This area for Control Click Response (UI Based)"

    Private Sub Ctl_U3DI_Status1_PortSelected(ByVal PortID As Integer) Handles Ctl_U3DI_Status1.PortSelected
        If InitDone Then
            EditDevice = 0
            EditPort = PortID
            ConfigForm.RefreshForm()
            ConfigForm.Visible = True
            ConfigForm.TopMost = True
        End If
        InitDone = True
    End Sub

    Private Sub Ctl_U3DI_Status2_PortSelected(ByVal PortID As Integer) Handles Ctl_U3DI_Status2.PortSelected
        EditDevice = 1
        EditPort = PortID
        ConfigForm.RefreshForm()
        ConfigForm.Visible = True
        ConfigForm.TopMost = True
    End Sub

    Private Sub Ctl_U3DI_Status3_PortSelected(ByVal PortID As Integer) Handles Ctl_U3DI_Status3.PortSelected
        EditDevice = 2
        EditPort = PortID
        ConfigForm.RefreshForm()
        ConfigForm.Visible = True
        ConfigForm.TopMost = True
    End Sub

    Private Sub Ctl_U3DI_Status4_PortSelected(ByVal PortID As Integer) Handles Ctl_U3DI_Status4.PortSelected
        EditDevice = 3
        EditPort = PortID
        ConfigForm.RefreshForm()
        ConfigForm.Visible = True
        ConfigForm.TopMost = True
    End Sub

    Private Sub Ctl_U3DI_Status5_PortSelected(ByVal PortID As Integer) Handles Ctl_U3DI_Status5.PortSelected
        EditDevice = 4
        EditPort = PortID
        ConfigForm.RefreshForm()
        ConfigForm.Visible = True
        ConfigForm.TopMost = True
    End Sub

    Private Sub Ctl_U3DI_Status6_PortSelected(ByVal PortID As Integer) Handles Ctl_U3DI_Status6.PortSelected
        EditDevice = 5
        EditPort = PortID
        ConfigForm.RefreshForm()
        ConfigForm.Visible = True
        ConfigForm.TopMost = True
    End Sub
#End Region

    Private Sub ConfigureForHomeSeer2ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfigureForHomeSeer2ToolStripMenuItem.Click
        With HS2Connect.Tables(0).Rows(0)
            H2Form.UserID = .Item("USerID")
            H2Form.Server = .Item("Server")
            H2Form.Password = .Item("Password")
            H2Form.Show()
            H2Form.TopMost = True
        End With
    End Sub

    Private Sub Ctl_U3DI_Status1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ctl_U3DI_Status1.Load

    End Sub
End Class
